package com.ruoyi.project.edu.specialized.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.edu.specialized.domain.Specialized;
import com.ruoyi.project.edu.specialized.service.ISpecializedService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 专业Controller
 * 
 * @author yueqiangu
 * @date 2021-03-01
 */
@Controller
@RequestMapping("/edu/specialized")
public class SpecializedController extends BaseController
{
    private String prefix = "edu/specialized";

    @Autowired
    private ISpecializedService specializedService;

    @RequiresPermissions("edu:specialized:view")
    @GetMapping()
    public String specialized()
    {
        return prefix + "/specialized";
    }

    /**
     * 查询专业树列表
     */
    @RequiresPermissions("edu:specialized:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Specialized> list(Specialized specialized)
    {
        List<Specialized> list = specializedService.selectSpecializedList(specialized);
        return list;
    }

    /**
     * 导出专业列表
     */
    @RequiresPermissions("edu:specialized:export")
    @Log(title = "专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Specialized specialized)
    {
        List<Specialized> list = specializedService.selectSpecializedList(specialized);
        ExcelUtil<Specialized> util = new ExcelUtil<Specialized>(Specialized.class);
        return util.exportExcel(list, "specialized");
    }

    /**
     * 新增专业
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("specialized", specializedService.selectSpecializedById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存专业
     */
    @RequiresPermissions("edu:specialized:add")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Specialized specialized)
    {
        return toAjax(specializedService.insertSpecialized(specialized));
    }

    /**
     * 修改专业
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Specialized specialized = specializedService.selectSpecializedById(id);
        mmap.put("specialized", specialized);
        return prefix + "/edit";
    }

    /**
     * 修改保存专业
     */
    @RequiresPermissions("edu:specialized:edit")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Specialized specialized)
    {
        return toAjax(specializedService.updateSpecialized(specialized));
    }

    /**
     * 删除
     */
    @RequiresPermissions("edu:specialized:remove")
    @Log(title = "专业", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(specializedService.deleteSpecializedById(id));
    }

    /**
     * 选择专业树
     */
    @GetMapping(value = { "/selectSpecializedTree/{id}", "/selectSpecializedTree/" })
    public String selectSpecializedTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("specialized", specializedService.selectSpecializedById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载专业树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = specializedService.selectSpecializedTree();
        return ztrees;
    }
}
